﻿using FlaUI.UIA3;
using System.Diagnostics;
using System;
using System.IO;
using FlaUI.Core;
using FlaUI.Core.AutomationElements;
using System.Linq;
using FlaUI.Core.Definitions;
using System.Threading;
public class WXWork5CoordinateMapper
{
    private Window _window;


    public bool Initialize()
    {
        try
        {
            System.Threading.Thread.Sleep(1000); // 5.0版本启动较慢


            var processes = Process.GetProcessesByName("WXWork");
            if (processes.Length == 0)
            {
                // 企业微信 5.0 常见安装路径
                string[] paths = {
                    @"D:\Program Files (x86)\WXWork\WXWork.exe",
                    Environment.ExpandEnvironmentVariables(@"%USERPROFILE%\AppData\Local\Programs\WXWork\WXWork.exe")
                };

                foreach (var path in paths)
                {
                    if (File.Exists(path))
                    {
                        var app = Application.Launch(path);
                        System.Threading.Thread.Sleep(8000); // 5.0版本启动较慢
                        break;
                    }
                }
            }

            processes = Process.GetProcessesByName("WXWork");
            if (processes.Length > 0)
            {
                var app = Application.Attach(processes[0]);
                using (var automation = new UIA3Automation())
                {
                    _window = app.GetMainWindow(automation);
                    return _window != null;
                }
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine($"初始化失败: {ex.Message}");
        }
        return false;
    }
    public WXWork5CoordinateMapper()
    {

    }
    public WXWork5CoordinateMapper(Window _window)
    {

    }
    public void ClickStandardLocations()
    {
        var bounds = _window.BoundingRectangle;
        Console.WriteLine($"企业微信 5.0 窗口尺寸: {bounds.Width}x{bounds.Height}");

        // 企业微信 5.0 标准界面布局坐标
        {
            var standardLocations = new[]
        {
            new { Name = "左侧-消息", X = 0.03, Y = 0.12 },
            new { Name = "左侧-邮件", X = 0.03, Y = 0.20 },
            new { Name = "左侧-文档", X = 0.03, Y = 0.28 },
            new { Name = "左侧-日程", X = 0.03, Y = 0.36 },
            new { Name = "左侧-会议", X = 0.03, Y = 0.44 },
            new { Name = "左侧-智能表格", X = 0.03, Y = 0.52 },
            new { Name = "左侧-工作台", X = 0.03, Y = 0.60 },
            new { Name = "左侧-通讯录", X = 0.03, Y = 0.68 },
            //微盘 76
            //智能总结 84
            //高级功能 92
            //高级功能 1
            new { Name = "左侧-通讯录", X = 0.03, Y = 0.68 },  


            //new { Name = "顶部-搜索框", X = 0.25, Y = 0.06 },   
            //new { Name = "搜索框区域", X = 0.30, Y = 0.06 },    
            //new { Name = "联系人列表", X = 0.15, Y = 0.25 },    
            //new { Name = "聊天输入框", X = 0.35, Y = 0.85 },    
            //new { Name = "发送按钮", X = 0.92, Y = 0.85 },      
            //new { Name = "菜单按钮", X = 0.97, Y = 0.06 },   
            
            new { Name = "最小化", X = 0.90, Y = 0.02 },
            new { Name = "最大化", X = 0.94, Y = 0.02 },
            new { Name = "关闭", X = 0.97, Y = 0.02 }
        };
            //foreach (var location in standardLocations)
            //{
            //    Thread.Sleep(2000);
            //    Console.WriteLine($"点击: {location.Name}");
            //    ClickRelative(location.X, location.Y);
            //    System.Threading.Thread.Sleep(1500);

            //    // 检查是否有新窗口弹出
            //    CheckForNewWindows();
            //}
        }
        {
            // 企业微信 5.0 标准界面布局坐标
            var standardLocations = new[]
            {
            new { Name = "左侧-消息", X = 60, Y = 78 },

            new { Name = "搜索框区域", X = 260, Y = 39 },

            new { Name = "左侧-邮件", X = 60, Y = 111 },
            new { Name = "左侧-文档", X = 60, Y = 144 },
            new { Name = "左侧-日程", X = 60, Y = 177 },
            new { Name = "左侧-会议", X = 60, Y = 210 },
            new { Name = "左侧-智能表格", X = 60, Y = 241 },
            new { Name = "左侧-工作台", X = 60, Y = 272 },
            new { Name = "左侧-通讯录", X = 60, Y = 305 },
            //微盘 76
            //智能总结 84
            //高级功能 92
            //高级功能 1
            //new { Name = "左侧-微盘", X = 60, Y = 336},  

            new { Name = "左侧-未读", X = 60, Y = 470},
            new { Name = "左侧-@我", X = 60, Y = 505},  

            //new { Name = "顶部-搜索框", X = 0.25, Y = 0.06 },   
            //new { Name = "搜索框区域", X = 0.30, Y = 0.06 },    
            //new { Name = "联系人列表", X = 0.15, Y = 0.25 },    
            //new { Name = "聊天输入框", X = 0.35, Y = 0.85 },    
            //new { Name = "发送按钮", X = 0.92, Y = 0.85 },      
            //new { Name = "菜单按钮", X = 0.97, Y = 0.06 },   
            
            //new { Name = "最小化", X = 1012, Y = 13 },
            //new { Name = "最大化", X = 1048, Y = 13 },
            //new { Name = "关闭", X = 1083, Y = 13 }
        };

            foreach (var location in standardLocations)
            {
                Console.WriteLine($"点击: {location.Name}");
                ClickRelative(location.X, location.Y);
                // 检查是否有新窗口弹出
                CheckForNewWindows();
            }

        }



    }

    public void ClickRelative(double relX, double relY)
    {
        var bounds = _window.BoundingRectangle;
        int x = (int)(bounds.Left + (relX > 1 ? relX : (bounds.Width * relX)));
        int y = (int)(bounds.Top + (relY > 1 ? relY : (bounds.Height * relY)));

        Console.WriteLine($"  相对位置 ({relX:P0}, {relY:P0}) -> 绝对位置 ({x}, {y})");

        SetCursorPos(x, y);
        System.Threading.Thread.Sleep(100);
        mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0);
        System.Threading.Thread.Sleep(50);
        mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0);
    }

    private void CheckForNewWindows()
    {
        try
        {
            using (var automation = new UIA3Automation())
            {
                var desktop = automation.GetDesktop();
                var windows = desktop.FindAllDescendants(cf => cf.ByControlType(ControlType.Window));

                foreach (var window in windows)
                {
                    if (!string.IsNullOrEmpty(window.Name) &&
                        window.Name.Contains("企业微信") &&
                        window != _window)
                    {
                        Console.WriteLine($"  发现新窗口: {window.Name}");
                    }
                }
            }
        }
        catch { }
    }

    [System.Runtime.InteropServices.DllImport("user32.dll")]
    static extern bool SetCursorPos(int X, int Y);

    [System.Runtime.InteropServices.DllImport("user32.dll")]
    static extern void mouse_event(uint dwFlags, uint dx, uint dy, uint cButtons, uint dwExtraInfo);

    const uint MOUSEEVENTF_LEFTDOWN = 0x0002;
    const uint MOUSEEVENTF_LEFTUP = 0x0004;
}